﻿Imports System.Data.SqlClient
Imports System.Xml
Imports System.Data.OleDb

Public Class PlayerListFeed
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Clear()
        Response.ContentType = "application/xml"
        Dim textWriter As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
        textWriter.WriteStartDocument()

        textWriter.WriteStartElement("rss")
        textWriter.WriteAttributeString("version", "2.0")

        textWriter.WriteStartElement("channel")

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
        oleDbConn.Open()

        Dim clubID As String = Request.Params("ClubID")
        Dim sqlString As String
        Dim cmd As OleDbCommand
        Dim dr As OleDbDataReader

        If Not String.IsNullOrEmpty(clubID) Then
            sqlString = "SELECT * FROM PlayerList WHERE Clubs.ID = @f1 ORDER BY CreatedAt DESC"
            cmd = New OleDbCommand(sqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", Request.Params("ClubID"))
            dr = cmd.ExecuteReader()
        Else
            sqlString = "SELECT * FROM PlayerList ORDER BY CreatedAt DESC"
            cmd = New OleDbCommand(sqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            dr = cmd.ExecuteReader()
        End If

        textWriter.WriteElementString("title", "Player List RSS Feed")
        textWriter.WriteElementString("link", "http://" & Request.Url.Host + ":" & Request.Url.Port & "/ClubList.aspx")
        textWriter.WriteElementString("description", "This feed include a list of football players added by users")
        textWriter.WriteElementString("language", "en-gb")
        textWriter.WriteElementString("ttl", "5")
        textWriter.WriteElementString("lastBuildDate", String.Format("{0:F}", DateTime.Now))

        Do While dr.Read()
            textWriter.WriteStartElement("item")
            textWriter.WriteElementString("title", dr("FullName").ToString())
            textWriter.WriteElementString("author", dr("Username").ToString())
            textWriter.WriteElementString("link", "http://" & Request.Url.Host + ":" & Request.Url.Port & "/PlayerDetail.aspx?PlayerID=" & dr("Players.ID").ToString())

            textWriter.WriteStartElement("guid")
            textWriter.WriteAttributeString("isPermaLink", "true")
            textWriter.WriteString("http://" & Request.Url.Host + ":" & Request.Url.Port & "/PlayerDetail.aspx?PlayerID=" & dr("Players.ID").ToString())

            textWriter.WriteEndElement()

            textWriter.WriteElementString("pubDate", String.Format("{0:F}", dr("CreatedAt")))

            textWriter.WriteElementString("description", dr("PositionName") & " - " & dr("ClubName"))
            textWriter.WriteEndElement()
        Loop

        textWriter.WriteEndElement()
        textWriter.WriteEndElement()

        textWriter.WriteEndDocument()
        textWriter.Flush()
        textWriter.Close()
        Response.End()

        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
    End Sub

End Class